package com.bilibili.juc.cf.one;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class FutureAllOf2Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<String> a = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t -----A come in " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("A我执行完了");
            System.out.println(Thread.currentThread().getName() + "\t -----A over " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            return "A任务的结果";
        });
        CompletableFuture<String> b = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t -----B come in " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("B我执行完了");
            System.out.println(Thread.currentThread().getName() + "\t -----B over " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            return "B任务的结果";
        });
        @SuppressWarnings("")
        CompletableFuture<Void> allOfFuture2 = CompletableFuture.allOf(a, b).whenComplete((m, k) -> {
            System.out.println("A and B all finish");
            System.out.println(Thread.currentThread().getName() + "\t -----allOf whenComplete " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        });
        System.out.println("all==> " + allOfFuture2.get());
        System.out.println("A==> " + a.get());
        System.out.println("B==> " + b.get());
    }
}
/*
ForkJoinPool.commonPool-worker-1	 -----A come in 2024-11-21 15:59:33
ForkJoinPool.commonPool-worker-2	 -----B come in 2024-11-21 15:59:33
B我执行完了
ForkJoinPool.commonPool-worker-2	 -----B over 2024-11-21 15:59:34
A我执行完了
ForkJoinPool.commonPool-worker-1	 -----A over 2024-11-21 15:59:35
A and B all finish
ForkJoinPool.commonPool-worker-1	 -----allOf whenComplete 2024-11-21 15:59:35
all==> null
A==> A任务的结果
B==> B任务的结果
 */
